package foodmaterial.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static String format(Date date,String formatter) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatter);
        return dtf.format(localDate) ;
    }

    public static DayOfWeek getWeek(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate.getDayOfWeek() ;
    }

    public static boolean isFriday(Date date) {
        DayOfWeek dayOfWeek = getWeek(date) ;
        if (dayOfWeek == DayOfWeek.FRIDAY) return true ;
        return false ;
    }

    public static Date parse(String date) {
        LocalDate localDate = LocalDate.parse(date);
        LocalDateTime localDateTime = localDate.atStartOfDay();
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()) ;
    }

    public static Date parse(String date, String formatter) {
        LocalDate localDate = LocalDate.parse(date,DateTimeFormatter.ofPattern(formatter));
        LocalDateTime localDateTime = localDate.atStartOfDay();
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()) ;
    }

    public static Date plus(Date date,int days) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        localDate = localDate.plusDays(days) ;
        LocalDateTime localDateTime = localDate.atStartOfDay();
        return Date.from(localDateTime.atZone(zoneId).toInstant()) ;
    }

    public static List<Date> dateRange(Date begin,Date end) {
        Instant instant = begin.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDate beginLocalDate = instant.atZone(zoneId).toLocalDate();
        instant = end.toInstant();
        LocalDate endLocalDate = instant.atZone(zoneId).toLocalDate();
        List<Date> list = new ArrayList<>() ;

        while (true) {
            if (beginLocalDate.isBefore(endLocalDate)) {
                LocalDateTime localDateTime = beginLocalDate.atStartOfDay();
                list.add(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())) ;
                beginLocalDate = beginLocalDate.plusDays(1) ;
            } else if (beginLocalDate.isEqual(endLocalDate)) {
                LocalDateTime localDateTime = beginLocalDate.atStartOfDay();
                list.add(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())) ;
                break ;
            } else {
                break ;
            }
        }
        return list ;
    }

}
